<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags"  %>
<!DOCTYPE html>
<html>
<head>
<jsp:include page="../inc.jsp"></jsp:include>
<script type="text/javascript" charset="utf-8">
	var type = "${sessionInfo.type}";
	var datagrid;
	var auth = "${auths}";
	var buttons = [];

	//查看详情
	function select(){

		var select = datagrid.datagrid('getSelected');
		//根据id号打开一个对话框
		if(select==null){$.messager.alert('警告',"请先选中一行！");return;}
		var id=select.id;
<%--		$("<div></div>").window({--%>
<%--			width:500,--%>
<%--			height:350,--%>
<%--			title:'查看详情',--%>
<%--			href:'${pageContext.request.contextPath}/qxgl/userAction!userSelect.html?id='+id,--%>
<%--			minimizable: false,--%>
<%--			maximizable: false,--%>
<%--			collapsible:false,--%>
<%--			onload:function(){--%>
<%--			　　//子页面的JS代码写在这里--%>
<%--				--%>
<%--			  	--%>
<%--			}});--%>

		var p = parent.sy.dialog({
			title : '查看详情',
			href : '${pageContext.request.contextPath}/qxgl/userAction!userSelect.html?id='+id,
			width : 550,
			height : 355});
		
		
	}
	
	$(function() {
		$('form input').bind('keyup', function(event) {/* 增加回车提交功能 */
			if (event.keyCode == '13') {
				datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
			}
		});
		if(auth=="all"){
			buttons.push({text : '增加',iconCls : 'icon-add',handler : function() {append();}});
			buttons.push('-');
			buttons.push({text : '删除',iconCls : 'icon-remove',handler : function() {remove();}});
			buttons.push('-');
			buttons.push({text : '编辑',iconCls : 'icon-edit',handler : function() {edit();}});
			buttons.push('-');
			buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
			buttons.push('-');
			buttons.push({text : '查看详情',iconCls : 'icon-edit',handler : function() {select();}});
		}else if(auth!=""){
			var auths = auth.split(",");
			if(auths.length>0){
				for(var i=0;i<auths.length;i++){
					if(auths[i]=="QXGL_YHGL_ADD"){
						buttons.push({text : '增加',iconCls : 'icon-add',handler : function() {append();}});
						buttons.push('-');
					}else if(auths[i]=="QXGL_YHGL_DELETE"){
						buttons.push({text : '删除',iconCls : 'icon-remove',handler : function() {remove();}});
						buttons.push('-');
					}else if(auths[i]=="QXGL_YHGL_EDIT"){
						buttons.push({text : '编辑',iconCls : 'icon-edit',handler : function() {edit();}});
						buttons.push('-');
					}
				}
				buttons.push({text : '取消选中',iconCls : 'icon-undo',handler : function() {datagrid.datagrid('clearSelections');datagrid.datagrid('unselectAll');}});
				buttons.push('-');
				buttons.push({text : '查看详情',iconCls : 'icon-edit',handler : function() {select();}});
			}
		}
		datagrid = $('#datagrid').datagrid({
			url : 'userAction!datagrid.html',
			title : '用户列表(admin拥有所有权限，不需要更改角色),本次更改下次登录时生效!',
			iconCls : 'icon-save',
			pagination : true,
			pagePosition : 'bottom',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40 ],
			fit : true,
			fitColumns : false,
			nowrap : false,
			border : false,
			idField : 'id',
			sortName : 'id',
			sortOrder : 'desc',
			checkOnSelect : false,
			selectOnCheck : false,
			singleSelect : true,
			columns : [ [{
				title : '编号',
				field : 'id',
				width : 100,
				sortable : true,
				checkbox : true
			}, {
				title : '用户名',
				field : 'username',
				width : 100,
				align:'center',
				sortable : true
			}, {
				title : '角色',
				field : 'roname',
				width : 100,
				align:'center',
			} , {
				title : '密码',
				field : 'password',
				width : 100,
				align:'center',
				hidden : true,
				formatter : function(value, rowData, rowIndex) {
					return '******';
				}
			},{
				title : '真实姓名',
				field : 'realName',
				sortable : true,
				width : 80,
				align:'center'
			},{
				title : '仓库ID',
				field : 'reid',
				sortable : true,
				hidden : true,
				width : 100
			},{
				title : '仓库',
				field : 'rename',
				width : 150,
				align:'center',
				hidden : true
			},{
				title : '手 机',
				field : 'mobile',
				sortable : true,
				width : 100,
				align:'center'
			},{
				title : '创建时间',
				field : 'createTime',
				sortable : true,
				width : 100,
				align:'center',
				hidden : true
			}, {
				title : '起始时间',
				field : 'validDate',
				sortable : true,
				width : 140,
				align:'center'
			},{
				title : '截止时间',
				field : 'expiryDate',
				sortable : true,
				width : 140,
				align:'center'
			},{
				title : '用户状态',
				field : 'status',
				sortable : true,
				hidden : true,
				width : 50,
				align:'center'
			},{
				title : '性 别',
				field : 'sex',
				sortable : true,
				width : 50,
				align:'center',
				hidden : true
			},{
				title : '生日',
				field : 'birthday',
				sortable : true,
				width : 140,
				align:'center'
			},{
				title : '雇佣性质',
				field : 'employType',
				sortable : true,
				width : 100,
				align:'center',
				hidden : true
			},{
				title : '部 门',
				field : 'dept',
				sortable : true,
				width : 100,
				align:'center',
				hidden : true
			},{
				title : '家庭地址',
				field : 'adress',
				sortable : true,
				width : 150,
				align:'center'
			},{
				title : '职务',
				field : 'job',
				sortable : true,
				width : 150,
				align:'center',
				hidden : true
			},{
				title : '家庭电话',
				field : 'tel',
				sortable : true,
				width : 100,
				align:'center',
				hidden : true
			},{
				title : '下级用户个数',
				field : 'lowerUserNum',
				width : 100,
				align:'center',
				hidden : true
			},{
				title : '仓库个数',
				field : 'repertoryNum',
				width : 100,
				align:'center',
				hidden : true
			}] ],
			toolbar : buttons,
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});

	});
	if(type!=0){
		$('#datagrid').datagrid('hideColumn','rename')
	}else{
		$('#datagrid').datagrid('showColumn','rename')
	}
	var roid0 = $('#searchForm').find('input[name=roid]');
	var rid0Combobox = roid0.combobox({
		url : '${pageContext.request.contextPath}/qxgl/roleAction!noAuth_combobox.html',
		valueField : 'id',
		textField : 'name',
		multiple : false,
		editable : false,
		panelHeight : 'auto',
		onLoadSuccess : function() {
			parent.$.messager.progress('close');
		}
	});
	
	function edit() {
		var rows = datagrid.datagrid('getSelections');
		if (rows.length == 1) {
			var p = parent.sy.dialog({
				title : '编辑用户',
				href : '${pageContext.request.contextPath}/qxgl/userAction!userEdit.html',
				width : 550,
				height : 355,
				buttons : [ {
					text : '编辑',
					handler : function() {
						var f = p.find('form');
						
						f.form('submit', {
							url : '${pageContext.request.contextPath}/qxgl/userAction!edit.html',
							success : function(d) {
								var json = $.parseJSON(d);
								if (json.success) {
									datagrid.datagrid('reload');
									p.dialog('close');
								}
								parent.sy.messagerShow({
									msg : json.msg,
									title : '提示'
								});
							}
						});
					}
				} ],
				onLoad : function() {
					var userType = rows[0].userType;
					var f = p.find('form');
					var roid = f.find('input[name=roid]');
					var roleIdsCombobox = roid.combobox({
						url : '${pageContext.request.contextPath}/qxgl/roleAction!noAuth_combobox.html?vRole.type='+userType,
						valueField : 'id',
						textField : 'name',
						multiple : false,
						editable : false,
						panelHeight : 'auto',
						onLoadSuccess : function() {
							parent.$.messager.progress('close');
						}
					});
					var reid = f.find('input[name=reid]');
					var rid0Combobox = reid.combobox({
						url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
						valueField : 'id',
						textField : 'name',
						multiple : false,
						editable : false,
						panelHeight : 'auto',
						onLoadSuccess : function() {
							parent.$.messager.progress('close');
						}
					});
					f.form('load', {
						id : rows[0].id,
						username : rows[0].username,
						roid : rows[0].roid,
						validDate : rows[0].validDate,
						expiryDate : rows[0].expiryDate,
						userType : rows[0].userType,
						reid : rows[0].reid,
						status : rows[0].status,
						realName : rows[0].realName,
						sex : rows[0].sex,
						mobile : rows[0].mobile,
						birthday : rows[0].birthday,
						employType : rows[0].employType,
						job : rows[0].job,
						dept : rows[0].dept,
						adress : rows[0].adress,
						tel : rows[0].tel,
						lowerUserNum: rows[0].lowerUserNum,
						repertoryNum: rows[0].repertoryNum
					});
				}
			});
		} else if (rows.length > 1) {
			parent.sy.messagerAlert('提示', '同一时间只能编辑一条记录！', 'error');
		} else {
			parent.sy.messagerAlert('提示', '请选择要编辑的记录！', 'error');
		}
	}
	function append() {
		var p = parent.sy.dialog({
			title : '添加用户',
			href : '${pageContext.request.contextPath}/qxgl/userAction!userAdd.html',
			width : 550,
			height : 325,
			buttons : [ {
				text : '添加',
				handler : function() {
					var f = p.find('form');
					var roid = f.find('input[name=roid]');
					if(roid.val()==null||roid.val()==""){
						//$.messager.alert("提示","请选择所属角色！");
						alert("请选择所属的角色！");
						return;
					}
					f.form('submit', {
						url : '${pageContext.request.contextPath}/qxgl/userAction!add.html',
						success : function(d) {
							var json = $.parseJSON(d);
							if (json.success) {
								datagrid.datagrid('reload');
								p.dialog('close');
							}
							parent.sy.messagerShow({
								msg : json.msg,
								title : '提示'
							});
						}
					});
				}
			} ],
			onLoad : function() {
				var userType = 1;
				if(type==1){
					userType = 3;
				}else if(type==3){
					userType = 4;
				}
				var f = p.find('form');
				var roid = f.find('input[name=roid]');
				var roleIdsCombobox = roid.combobox({
					url : '${pageContext.request.contextPath}/qxgl/roleAction!noAuth_combobox.html?vRole.type='+userType,
					valueField : 'id',
					textField : 'name',
					multiple : false,
					editable : false,
					panelHeight : 'auto',
					onLoadSuccess : function() {
						parent.$.messager.progress('close');
					}
				});
				var reid = f.find('input[name=reid]');
				var rid0Combobox = reid.combobox({
					url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
					valueField : 'id',
					textField : 'name',
					multiple : false,
					editable : false,
					panelHeight : 'auto',
					onLoadSuccess : function() {
						parent.$.messager.progress('close');
					}
				});
			}
		});
	}
	function remove() {
		var rows = datagrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.sy.messagerConfirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						ids.push(rows[i].id);
					}
					$.ajax({
						url : '${pageContext.request.contextPath}/qxgl/userAction!delete.html',
						data : {
							ids : ids.join(',')
						},
						dataType : 'json',
						success : function(d) {
							datagrid.datagrid('load');
							datagrid.datagrid('unselectAll');
							parent.sy.messagerShow({
								title : '提示',
								msg : d.msg
							});
						}
					});
				}
			});
		} else {
			parent.sy.messagerAlert('提示', '请勾选要删除的记录！', 'error');
		}
	}
	function _search() {
		datagrid.datagrid('load', sy.serializeObject($('#searchForm')));
	}
	function cleanSearch() {
		datagrid.datagrid('load', {});
		$('#searchForm input').val('');
	}
</script>
</head>
<body class="easyui-layout" data-options="fit:true">
	<div data-options="region:'north',border:false,title:'搜索条件'" style="height: 55px;overflow: hidden;" align="left">
		<form id="searchForm">
		
			<table class="tableForm datagrid-toolbar" style="width: 100%;height: 100%;">
				<tr>
					<th>用户名</th>
					<td><input name="username" style="width:150px;" /></td>
					<th>创建时间</th>
					<td><input name="createTimeStart" class="easyui-datetimebox" data-options="editable:false" style="width: 150px;" />至<input name="createTimeEnd" class="easyui-datetimebox" data-options="editable:false" style="width: 150px;" /></td>
					<td><a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">搜索</a> <a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a></td>
				</tr>
				
				<tr>
					
				</tr>
			</table>
		</form>
	</div>
	<div data-options="region:'center',border:false" style="overflow: hidden;">
		<table id="datagrid"></table>
	</div>

	<div id="menu" class="easyui-menu" style="width:120px;display: none;">
		<k:security key="QXGL_YHGL_ADD">
			<div onclick="append();" data-options="iconCls:'icon-add'">增加</div>
		</k:security>
		<k:security key="QXGL_YHGL_DELETE">
			<div onclick="remove();" data-options="iconCls:'icon-remove'">删除</div>
		</k:security>
		<k:security key="QXGL_YHGL_EDIT">
			<div onclick="edit();" data-options="iconCls:'icon-edit'">编辑</div>
		</k:security>
	</div>
</body>
</html>